<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>keepalived之vrrp_script总结 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>keepalived之vrrp_script总结</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LVS/">LVS</a> <a class="article__tag-link" href="/tags/keepalived/">keepalived</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通常情况下，利用keepalived做热备，其中一台设置为master，一台设置为backup。当master出现异常后，backup自动切换为master。当backup成为master后，master恢复正常后会再次抢占成为master，导致不必要的主备切换。因此可以将两台keepalived初始状态均配置为backup，设置不同的优先级，优先级高的设置nopreempt解决异常恢复后再次抢占的问题。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然而keepalived只能做到对网络故障和keepalived本身的监控，即当出现网络故障或    者keepalived本身出现问题时，进行切换。但是这些还不够，我们还需要监控    keepalived所在服务器上的其他业务进程，根据业务进程的运行状态决定是否需要进行主备切换。这个时候，我们可以通过编写脚本对业务进程进行检测监控。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如编写个简单脚本查看haproxy进程是否存活</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">count = `ps aux | grep -v grep | grep haproxy | wc -l`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$count</span> &gt; 0 ]; <span class="keyword">then</span></div><div class="line">    <span class="built_in">exit</span> 0</div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在keepalived的配置文件中增加相应配置项</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">vrrp_script checkhaproxy</div><div class="line">&#123;</div><div class="line">    script <span class="string">"/home/check.sh"</span></div><div class="line">    interval 3</div><div class="line">    weight -20</div><div class="line">&#125;</div><div class="line"></div><div class="line">vrrp_instance <span class="built_in">test</span></div><div class="line">&#123;</div><div class="line">    ...</div><div class="line">    </div><div class="line">    track_script</div><div class="line">    &#123;</div><div class="line">        checkhaproxy</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    ...</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived会定时执行脚本并对脚本执行的结果进行分析，动态调整vrrp_instance的优先级。</p>
<ul>
<li>如果脚本执行结果为0，并且weight配置的值大于0，则优先级相应的增加</li>
<li>如果脚本执行结果非0，并且weight配置的值小于0，则优先级相应的减少</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其他情况，维持原本配置的优先级，即配置文件中priority对应的值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里需要注意的是：</p>
<ul>
<li>优先级不会不断的提高或者降低</li>
<li>可以编写多个检测脚本并为每个检测脚本设置不同的weight</li>
<li>不管提高优先级还是降低优先级，最终优先级的范围是在[1,254]，不会出现优先级小于等于0或者优先级大于等于255的情况</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样可以做到利用脚本检测业务进程的状态，并动态调整优先级从而实现主备切换。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但是利用该方式会存在一个问题，例如：A,B两台keepalived</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;A的配置大概为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">vrrp_script checkhaproxy</div><div class="line">&#123;</div><div class="line">    script <span class="string">"/etc/check.sh"</span></div><div class="line">    interval 3</div><div class="line">    weight -20</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line">vrrp_instance <span class="built_in">test</span></div><div class="line">&#123;</div><div class="line">    ....</div><div class="line">    </div><div class="line">    state backup</div><div class="line">    priority 80</div><div class="line">    nopreempt</div><div class="line"></div><div class="line">    track_script</div><div class="line">    &#123;</div><div class="line">        checkhaproxy</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    ....</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;B的配置大概为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">vrrp_script checkhaproxy</div><div class="line">&#123;</div><div class="line">    script <span class="string">"/etc/check.sh"</span></div><div class="line">    interval 3</div><div class="line">    weight -20</div><div class="line">&#125;</div><div class="line"></div><div class="line">vrrp_instance <span class="built_in">test</span></div><div class="line">&#123;</div><div class="line">    ....</div><div class="line">    </div><div class="line">    state backup</div><div class="line">    priority 70</div><div class="line"></div><div class="line">    track_script</div><div class="line">    &#123;</div><div class="line">        checkhaproxy</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    ....</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;A,B同时启动后，由于A的优先级较高，因此通过选举会成为master。当A上的业务进程出现问题时，优先级会降低到60。此时B收到优先级比自己低的vrrp广播包时，将切换为master状态。那么当B上的业务出现问题时，优先级降低到50，尽管A的优先级比B的要    高，但是由于设置了nopreempt，A不会再抢占成为master状态。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所以，可以在检测脚本中增加杀掉keepalived进程（或者停用keepalived服务）的方式，做到业务进程出现问题时完成主备切换。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/LVS/22. keepalived之vrrp_script总结/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
